using System;
using Firestarter.Test.Base;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{
    /// <summary>
    /// Provides a place to write custom tests for DMSortOrder objects.
    /// This file is only written once and can be changed.  The Def file
    /// attached to this as a dependent is rewritten with each regeneration
    /// and contains the standard tests for DMSortOrder.
    /// Regenerate this test project whenever there have been changes to the
    /// business objects.
    /// If tests are failing due to a unique setup in your application,
    /// you can either override the Create methods in TestUtils, or you
    /// can add the test to the ignore list below and reimplement it here.
    /// </summary>
    public partial class TestDMSortOrder
    {
        [TestFixtureSetUp]
        public void SetupTestFixture()
        {
            TestUtilsDomain.SetupFixture();

            //------------------------------------------------------------
            // Use this list to ignore generated tests that are failing
            // due to a unique condition in your application.
            // Remember to reimplement the test here.
            //------------------------------------------------------------
            //_ignoreList.Add("TestMethodName", "Reason for ignoring it");
            //_ignoreList.Add("Test_ReadWriteRules", "Reason for ignoring it");
        }

        [SetUp]
        public void Setup()
        {
            TestUtilsDomain.SetupTest();
        }

        [Test]
        public void Test_ToString_WhenNoSortProperties_ShouldReturnEmptyString()
        {
            //---------------Set up test pack-------------------
            BORegistry.DataAccessor = new DataAccessorInMemory();
            DMSortOrder dMSortOrder = TestUtilsDMSortOrder.CreateUnsavedValidDMSortOrder();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            string toStringValue = dMSortOrder.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual("", toStringValue);
        }

        [Test]
        public void Test_ToString_WhenOneAscendingSortProperty_ShouldReturnPropertyOnly()
        {
            //---------------Set up test pack-------------------
            BORegistry.DataAccessor = new DataAccessorInMemory();
            DMSortProperty dmSortProperty = TestUtilsDMSortProperty.CreateUnsavedValidDMSortProperty();
            dmSortProperty.SortDirection = "ASC";
            DMSortOrder dMSortOrder = dmSortProperty.DMSortOrder;
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, dMSortOrder.DMSortProperties.Count);
            //---------------Execute Test ----------------------
            string toStringValue = dMSortOrder.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual(dmSortProperty.PropertyName, toStringValue);
        }

        [Test]
        public void Test_ToString_WhenOneDescendingSortProperty_ShouldReturnPropertyAndDirection()
        {
            //---------------Set up test pack-------------------
            BORegistry.DataAccessor = new DataAccessorInMemory();
            DMSortProperty dmSortProperty = TestUtilsDMSortProperty.CreateUnsavedValidDMSortProperty();
            dmSortProperty.SortDirection = "DESC";
            DMSortOrder dMSortOrder = dmSortProperty.DMSortOrder;
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, dMSortOrder.DMSortProperties.Count);
            //---------------Execute Test ----------------------
            string toStringValue = dMSortOrder.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual(dmSortProperty.PropertyName + " " + dmSortProperty.SortDirection, toStringValue);
        }

        [Test]
        public void Test_ToString_WhenManySortProperties_ShouldReturnOrderedListOfPropertyNamesAndDirections()
        {
            //---------------Set up test pack-------------------
            BORegistry.DataAccessor = new DataAccessorInMemory();
            DMSortOrder dMSortOrder = TestUtilsDMSortOrder.CreateUnsavedValidDMSortOrder();
            DMSortProperty dmSortProperty3 = TestUtilsDMSortProperty.CreateUnsavedValidDMSortProperty(dMSortOrder,3);
            DMSortProperty dmSortProperty1 = TestUtilsDMSortProperty.CreateUnsavedValidDMSortProperty(dMSortOrder,1);
            DMSortProperty dmSortProperty4 = TestUtilsDMSortProperty.CreateUnsavedValidDMSortProperty(dMSortOrder,4);
            DMSortProperty dmSortProperty2 = TestUtilsDMSortProperty.CreateUnsavedValidDMSortProperty(dMSortOrder,2);
            //---------------Assert Precondition----------------
            Assert.AreEqual(4, dMSortOrder.DMSortProperties.Count);
            //---------------Execute Test ----------------------
            string toStringValue = dMSortOrder.ToString();
            //---------------Test Result -----------------------
            string expected = dmSortProperty1.PropertyName + " " + dmSortProperty1.SortDirection + ", " +
                dmSortProperty2.PropertyName + " " + dmSortProperty2.SortDirection + ", " +
                dmSortProperty3.PropertyName + " " + dmSortProperty3.SortDirection + ", " +
                dmSortProperty4.PropertyName + " " + dmSortProperty4.SortDirection;
            Assert.AreEqual(expected, toStringValue);
        }

        [Test]
        public void Test_FromString_EmptyString()
        {
            //---------------Set up test pack-------------------
            const string sortProperty = "";
            //---------------Execute Test ----------------------
            DMSortOrder sortOrder = DMSortOrder.FromString(sortProperty);
            //---------------Test Result -----------------------
            Assert.AreEqual(0, sortOrder.DMSortProperties.Count);
        }

        [Test]
        public void Test_FromString_OneProperty()
        {
            //---------------Set up test pack-------------------
            const string sortProperty = "MyProp";
            //---------------Execute Test ----------------------
            DMSortOrder sortOrder = DMSortOrder.FromString(sortProperty);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, sortOrder.DMSortProperties.Count);
            Assert.AreEqual(sortProperty, sortOrder.DMSortProperties[0].PropertyName);
            Assert.AreEqual("ASC", sortOrder.DMSortProperties[0].SortDirection);
            Assert.AreEqual(1, sortOrder.DMSortProperties[0].SortPosition);
        }

        [Test]
        public void Test_FromString_OnePropertyDesc()
        {
            //---------------Set up test pack-------------------
            const string sortProperty = "MyProp";
            //---------------Execute Test ----------------------
            DMSortOrder sortOrder = DMSortOrder.FromString(sortProperty + " DESC");
            //---------------Test Result -----------------------
            Assert.AreEqual(1, sortOrder.DMSortProperties.Count);
            Assert.AreEqual(sortProperty, sortOrder.DMSortProperties[0].PropertyName);
            Assert.AreEqual("DESC", sortOrder.DMSortProperties[0].SortDirection);
            Assert.AreEqual(1, sortOrder.DMSortProperties[0].SortPosition);
        }
        [Test]
        public void Test_FromString_WhenDesc_ShouldStripDesc()
        {
            //---------------Set up test pack-------------------
            const string sortProperty = "MyProp";
            //---------------Execute Test ----------------------
            DMSortOrder sortOrder = DMSortOrder.FromString(sortProperty + " desc");
            //---------------Test Result -----------------------
            Assert.AreEqual(1, sortOrder.DMSortProperties.Count);
            Assert.AreEqual(sortProperty, sortOrder.DMSortProperties[0].PropertyName);
            Assert.AreEqual("DESC", sortOrder.DMSortProperties[0].SortDirection);
            Assert.AreEqual(1, sortOrder.DMSortProperties[0].SortPosition);
        }

        [Test]
        public void Test_FromString_WhenAsc_ShouldStripAsc()
        {
            //---------------Set up test pack-------------------
            const string sortProperty = "MyProp";
            //---------------Execute Test ----------------------
            DMSortOrder sortOrder = DMSortOrder.FromString(sortProperty + " asc");
            //---------------Test Result -----------------------
            Assert.AreEqual(1, sortOrder.DMSortProperties.Count);
            Assert.AreEqual(sortProperty, sortOrder.DMSortProperties[0].PropertyName);
            Assert.AreEqual("ASC", sortOrder.DMSortProperties[0].SortDirection);
            Assert.AreEqual(1, sortOrder.DMSortProperties[0].SortPosition);
        }

        [Test]
        public void Test_FromString_WhenPropNameEndsWithAsc_ShouldNotStripAsc()
        {
            //---------------Set up test pack-------------------
            const string sortProperty = "MyPropASC";
            //---------------Execute Test ----------------------
            DMSortOrder sortOrder = DMSortOrder.FromString(sortProperty);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, sortOrder.DMSortProperties.Count);
            Assert.AreEqual(sortProperty, sortOrder.DMSortProperties[0].PropertyName);
            Assert.AreEqual("ASC", sortOrder.DMSortProperties[0].SortDirection);
            Assert.AreEqual(1, sortOrder.DMSortProperties[0].SortPosition);
        }
        [Test]
        public void Test_FromString_WhenPropNameEndsWithDesc_ShouldNotStripAsc()
        {
            //---------------Set up test pack-------------------
            const string sortProperty = "MyPropDESC";
            //---------------Execute Test ----------------------
            DMSortOrder sortOrder = DMSortOrder.FromString(sortProperty);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, sortOrder.DMSortProperties.Count);
            Assert.AreEqual(sortProperty, sortOrder.DMSortProperties[0].PropertyName);
            Assert.AreEqual("ASC", sortOrder.DMSortProperties[0].SortDirection);
            Assert.AreEqual(1, sortOrder.DMSortProperties[0].SortPosition);
        }

        [Test]
        public void Test_FromString_WhenASC_ShouldStripASC()
        {
            //---------------Set up test pack-------------------
            const string sortProperty = "MyProp";
            //---------------Execute Test ----------------------
            DMSortOrder sortOrder = DMSortOrder.FromString(sortProperty + " ASC");
            //---------------Test Result -----------------------
            Assert.AreEqual(1, sortOrder.DMSortProperties.Count);
            Assert.AreEqual(sortProperty, sortOrder.DMSortProperties[0].PropertyName);
            Assert.AreEqual("ASC", sortOrder.DMSortProperties[0].SortDirection);
            Assert.AreEqual(1, sortOrder.DMSortProperties[0].SortPosition);
        }

        [Test]
        public void Test_FromString_TwoProperties()
        {
            //---------------Set up test pack-------------------
            const string sortProperty1 = "MyProp";
            const string sortProperty2 = "MyProp2";
            string sortOrderString = String.Format("{0}, {1}", sortProperty1, sortProperty2);
            //---------------Execute Test ----------------------
            DMSortOrder sortOrder = DMSortOrder.FromString(sortOrderString);
            //---------------Test Result -----------------------
            Assert.AreEqual(2, sortOrder.DMSortProperties.Count);
            Assert.AreEqual(sortProperty1, sortOrder.DMSortProperties[0].PropertyName);
            Assert.AreEqual("ASC", sortOrder.DMSortProperties[0].SortDirection);
            Assert.AreEqual(1, sortOrder.DMSortProperties[0].SortPosition);
            Assert.AreEqual(sortProperty2, sortOrder.DMSortProperties[1].PropertyName);
            Assert.AreEqual("ASC", sortOrder.DMSortProperties[1].SortDirection);
            Assert.AreEqual(2, sortOrder.DMSortProperties[1].SortPosition);
        }



    }
}